﻿import QtQuick 2.0
import QtQuick.Templates 2.12
import QtGraphicalEffects 1.14

Popup{              // 弹窗组件 (Android端Windows不好使)
    id: root

    property color overlayColor: "#14444444"

    // 显示属性
    parent: Overlay.overlay

    x: (parent.width - width) * 0.5
    y: (parent.height - height) * 0.5

    onVisibleChanged: {
//        if(visible){
//            root.x = (parent.width - width) * 0.5
//            root.y = (parent.height - height) * 0.5
//        }
    }

    property bool isMovable: false

    modal: true
    Overlay.modal: Rectangle{
        color: overlayColor
    }
    closePolicy: Popup.NoAutoClose
    property bool showCloseButton: false

    function show(){
        root.visible = true
    }function close(){
        root.visible = false
    }


    // 样式
    height: 500*dp
    width: 700*dp
    property string title: "提示"

    padding: 10*dp
    topPadding: 40*dp
    // 背景
    background: Rectangle{
        radius: 8*dp
        anchors.fill: parent
        Item{       // 标题行
            id: item_title
            property bool isPressed: false
            property bool isHovered: false
            height: 40*dp; width: parent.width
            MouseArea{      // 窗口可拖动
                anchors.fill: parent
                property var lastPos: 0
                hoverEnabled: config.hoverEnabled
                onPressed: {
                    if(!isMovable)  return
                    item_title.isPressed = true
                    var mousePos = mapToItem(root.parent, mouseX, mouseY)
                    lastPos = mousePos
                }
                onEntered: {
                    if(!isMovable)  return
                    item_title.isHovered = true
                }onExited: {
                    if(!isMovable)  return
                    item_title.isHovered = false
                }
                onPositionChanged: {
                    if(!isMovable)  return
                    if(!item_title.isPressed){
                        return
                    }
                    var mousePos = mapToItem(root.parent, mouseX, mouseY)
                    root.x = Math.min(root.parent.width-width, Math.max(0, root.x+mousePos.x-lastPos.x))
                    root.y = Math.min(root.parent.height-height, Math.max(0, root.y+mousePos.y-lastPos.y))
                    lastPos = mousePos
                }onReleased: {
                    if(!isMovable)  return
                    item_title.isPressed = false
                }
            }
            Rectangle{      // 可拖动时显示一个透明的小黑条条
                visible: item_title.isPressed || item_title.isHovered
                anchors.fill: parent
                color: "#09111111"
            }
            Text{       // 标题
                text: title
                x: 12*dp
                color: "#888888"
                font.pixelSize: config.fontsize
                anchors.verticalCenter: parent.verticalCenter
            }
            Item{       // 关闭按钮
                visible: showCloseButton      // 关闭按钮不显示
                height: 20*dp; width: 20*dp
                x: parent.width - width - 12*dp
                anchors.verticalCenter: parent.verticalCenter
                MouseArea{
                    anchors.fill: parent
                    cursorShape: Qt.PointingHandCursor
                    onClicked: {
                        root.visible = false
                    }
                }Text{
                    text: "x"
                    anchors.centerIn: parent
                    font.pixelSize: 18*dp
                    color: "#888888"
                }
            }
        }

        // 阴影
        layer.enabled: true
        layer.effect: DropShadow {
            radius: 21*dp
            samples: 43
            transparentBorder: true
            color: "#4D666666"
        }
    }
    // 内容
    contentItem: Item{

    }

}
